﻿using Iworld.Author.Model;
using Iworld.Author.Service.Backstage.Export;
using Iworld.basis.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Author.Service.Backstage.Import
{
    /// <summary>
    /// 获取用户登录记录
    /// </summary>
    [DataContract]
    public class GetLandingRecordsImport : GetPageListCheckAdministratorImportBase
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        [DataMember]
        public DateTime? BeginTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        [DataMember]
        public DateTime? EndTime { get; set; }

         [DataMember]
        public string Keyword { set; get; }

        public PageResult<AuthorLandingRecordExport> GetLandingRecords(IModelToDbContextOfAuthor db)
        {
            int pageSize = 10;
            int startRow = pageSize * (this.PageIndex - 1);
            IQueryable<AuthorLandingRecord> temp = db.AuthorLandingRecords;
            if (BeginTime != null)
            {
                temp = temp.Where(x => x.CreatedTime > (DateTime)BeginTime);
            }
            if (EndTime != null)
            {
                temp = temp.Where(x => x.CreatedTime < (DateTime)EndTime);
            }
            if (Keyword != null && Keyword.Trim().Length > 0)
            {
                temp = temp.Where(x => x.Owner.Username.Contains(Keyword)
                    || x.Owner.Account.Contains(Keyword));
            }
            int totalCount = temp.Count();
            var list = temp.OrderByDescending(x => x.CreatedTime).Skip(startRow).Take(pageSize).ToList().ConvertAll(x =>
            {
                return new AuthorLandingRecordExport(x);
            });

            return new PageResult<AuthorLandingRecordExport>(this.PageIndex, totalCount, pageSize, list);
        }
    }
}
